#include "../src/wal/wal_manager.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <inttypes.h>

// Test helper functions
static void print_test_header(const char* test_name);
static void print_test_result(const char* test_name, bool passed);
static int cleanup_test_directory(const char* path);
static int create_test_directory(const char* path);

static void print_test_header(const char* test_name) {
    printf("\n=== %s ===\n", test_name);
}

static void print_test_result(const char* test_name, bool passed) {
    printf("%s: %s\n", test_name, passed ? "PASSED" : "FAILED");
}

static int cleanup_test_directory(const char* path) {
    char command[256];
    snprintf(command, sizeof(command), "rm -rf %s", path);
    return system(command);
}

static int create_test_directory(const char* path) {
    cleanup_test_directory(path);
    return mkdir(path, 0755);
}

// Test WAL manager creation and destruction
static bool test_wal_manager_creation(void) {
    print_test_header("Testing WAL Manager Creation");
    
    const char* test_wal_dir = "/tmp/test_wal_creation";
    create_test_directory(test_wal_dir);
    
    wal_manager_t* wal = wal_manager_create(test_wal_dir);
    if (!wal) {
        printf("Failed to create WAL manager\n");
        cleanup_test_directory(test_wal_dir);
        return false;
    }
    
    printf("Created WAL manager successfully\n");
    printf("  WAL directory: %s\n", wal->wal_directory);
    printf("  Segment size: %u bytes\n", wal->segment_size);
    printf("  Buffer size: %u bytes\n", wal->buffer_size);
    printf("  Sync interval: %u ms\n", wal->sync_interval_ms);
    printf("  Checkpoint interval: %u sec\n", wal->checkpoint_interval_sec);
    printf("  Next LSN: %" PRIu64 "\n", wal->next_lsn);
    
    wal_manager_destroy(wal);
    printf("Destroyed WAL manager successfully\n");
    
    cleanup_test_directory(test_wal_dir);
    return true;
}

// Test WAL manager configuration
static bool test_wal_manager_configuration(void) {
    print_test_header("Testing WAL Manager Configuration");
    
    const char* test_wal_dir = "/tmp/test_wal_config";
    create_test_directory(test_wal_dir);
    
    wal_manager_t* wal = wal_manager_create(test_wal_dir);
    if (!wal) {
        printf("Failed to create WAL manager\n");
        cleanup_test_directory(test_wal_dir);
        return false;
    }
    
    bool all_passed = true;
    
    // Test segment size configuration
    printf("Testing segment size configuration:\n");
    if (wal_set_segment_size(wal, 32 * 1024 * 1024) == 0) {
        printf("  Set segment size to 32MB: SUCCESS\n");
        if (wal->segment_size != 32 * 1024 * 1024) {
            printf("  Segment size not updated correctly\n");
            all_passed = false;
        }
    } else {
        printf("  Failed to set segment size\n");
        all_passed = false;
    }
    
    // Test buffer size configuration
    printf("Testing buffer size configuration:\n");
    if (wal_set_buffer_size(wal, 2 * 1024 * 1024) == 0) {
        printf("  Set buffer size to 2MB: SUCCESS\n");
        if (wal->buffer_size != 2 * 1024 * 1024) {
            printf("  Buffer size not updated correctly\n");
            all_passed = false;
        }
    } else {
        printf("  Failed to set buffer size\n");
        all_passed = false;
    }
    
    // Test sync interval configuration
    printf("Testing sync interval configuration:\n");
    if (wal_set_sync_interval(wal, 2000) == 0) {
        printf("  Set sync interval to 2000ms: SUCCESS\n");
        if (wal->sync_interval_ms != 2000) {
            printf("  Sync interval not updated correctly\n");
            all_passed = false;
        }
    } else {
        printf("  Failed to set sync interval\n");
        all_passed = false;
    }
    
    // Test checkpoint interval configuration
    printf("Testing checkpoint interval configuration:\n");
    if (wal_set_checkpoint_interval(wal, 600) == 0) {
        printf("  Set checkpoint interval to 600sec: SUCCESS\n");
        if (wal->checkpoint_interval_sec != 600) {
            printf("  Checkpoint interval not updated correctly\n");
            all_passed = false;
        }
    } else {
        printf("  Failed to set checkpoint interval\n");
        all_passed = false;
    }
    
    // Test invalid configurations
    printf("Testing invalid configurations:\n");
    if (wal_set_segment_size(wal, 1024) != 0) {
        printf("  Correctly rejected too small segment size\n");
    } else {
        printf("  Should have rejected too small segment size\n");
        all_passed = false;
    }
    
    if (wal_set_buffer_size(wal, 1024) != 0) {
        printf("  Correctly rejected too small buffer size\n");
    } else {
        printf("  Should have rejected too small buffer size\n");
        all_passed = false;
    }
    
    wal_manager_destroy(wal);
    cleanup_test_directory(test_wal_dir);
    
    return all_passed;
}

// Test WAL record creation and management
static bool test_wal_record_management(void) {
    print_test_header("Testing WAL Record Management");
    
    bool all_passed = true;
    
    // Test record creation
    printf("Testing record creation:\n");
    const char* test_data = "Hello, WAL World!";
    uint32_t data_size = strlen(test_data);
    
    wal_record_t* record = wal_record_create(WAL_INSERT, 123, test_data, data_size);
    if (!record) {
        printf("Failed to create WAL record\n");
        return false;
    }
    
    printf("  Created WAL record successfully\n");
    printf("    Type: %s\n", wal_record_type_to_string(record->header.type));
    printf("    XID: %" PRIu64 "\n", record->header.xid);
    printf("    Length: %u\n", record->header.length);
    printf("    Data size: %u\n", record->data_size);
    
    // Verify data
    if (record->data_size != data_size) {
        printf("  Data size mismatch\n");
        all_passed = false;
    }
    
    if (memcmp(record->data, test_data, data_size) != 0) {
        printf("  Data content mismatch\n");
        all_passed = false;
    } else {
        printf("  Data content matches: SUCCESS\n");
    }
    
    // Test record verification (simplified)
    printf("Testing record verification:\n");
    record->header.crc32 = 0x12345678;  // Set a dummy CRC
    printf("  Record verification: %s\n", 
           wal_verify_record(record) ? "PASSED" : "FAILED (expected)");
    
    wal_record_destroy(record);
    printf("  Destroyed record successfully\n");
    
    // Test empty record
    printf("Testing empty record creation:\n");
    wal_record_t* empty_record = wal_record_create(WAL_CHECKPOINT, 0, NULL, 0);
    if (!empty_record) {
        printf("  Failed to create empty record\n");
        all_passed = false;
    } else {
        printf("  Created empty record successfully\n");
        printf("    Type: %s\n", wal_record_type_to_string(empty_record->header.type));
        printf("    Data size: %u\n", empty_record->data_size);
        wal_record_destroy(empty_record);
    }
    
    return all_passed;
}

// Test WAL buffer operations
static bool test_wal_buffer_operations(void) {
    print_test_header("Testing WAL Buffer Operations");
    
    bool all_passed = true;
    
    // Create buffer
    printf("Testing buffer creation:\n");
    wal_buffer_t* buffer = wal_buffer_create(4096);
    if (!buffer) {
        printf("Failed to create WAL buffer\n");
        return false;
    }
    
    printf("  Created buffer successfully\n");
    printf("    Capacity: %u bytes\n", buffer->capacity);
    printf("    Used: %u bytes\n", buffer->used);
    
    // Test buffer append
    printf("Testing buffer append:\n");
    const char* test_data = "Test data for buffer";
    wal_record_t* record = wal_record_create(WAL_INSERT, 100, test_data, strlen(test_data));
    if (!record) {
        printf("  Failed to create test record\n");
        wal_buffer_destroy(buffer);
        return false;
    }
    
    // Set record header fields for testing
    record->header.lsn = 1;
    record->header.length = sizeof(wal_record_header_t) + strlen(test_data);
    record->header.timestamp = 1234567890;
    record->header.crc32 = 0xABCDEF;
    
    if (wal_buffer_append(buffer, record) == 0) {
        printf("  Appended record to buffer: SUCCESS\n");
        printf("    Buffer used after append: %u bytes\n", buffer->used);
        
        uint32_t expected_used = sizeof(wal_record_header_t) + strlen(test_data);
        if (buffer->used != expected_used) {
            printf("  Buffer usage incorrect (expected %u, got %u)\n", 
                   expected_used, buffer->used);
            all_passed = false;
        }
    } else {
        printf("  Failed to append record to buffer\n");
        all_passed = false;
    }
    
    // Test multiple appends
    printf("Testing multiple appends:\n");
    for (int i = 0; i < 5; i++) {
        char data[32];
        snprintf(data, sizeof(data), "Record %d", i);
        
        wal_record_t* test_record = wal_record_create(WAL_UPDATE, 200 + i, data, strlen(data));
        if (test_record) {
            test_record->header.lsn = 2 + i;
            test_record->header.length = sizeof(wal_record_header_t) + strlen(data);
            test_record->header.timestamp = 1234567890 + i;
            test_record->header.crc32 = 0x1000 + i;
            
            if (wal_buffer_append(buffer, test_record) == 0) {
                printf("    Appended record %d: SUCCESS\n", i);
            } else {
                printf("    Failed to append record %d\n", i);
                all_passed = false;
            }
            wal_record_destroy(test_record);
        }
    }
    
    printf("  Final buffer usage: %u bytes\n", buffer->used);
    
    wal_record_destroy(record);
    wal_buffer_destroy(buffer);
    printf("  Destroyed buffer successfully\n");
    
    return all_passed;
}

// Test basic WAL operations
static bool test_basic_wal_operations(void) {
    print_test_header("Testing Basic WAL Operations");
    
    const char* test_wal_dir = "/tmp/test_wal_basic";
    create_test_directory(test_wal_dir);
    
    wal_manager_t* wal = wal_manager_create(test_wal_dir);
    if (!wal) {
        printf("Failed to create WAL manager\n");
        cleanup_test_directory(test_wal_dir);
        return false;
    }
    
    bool all_passed = true;
    
    // Start WAL manager
    printf("Starting WAL manager:\n");
    if (wal_manager_start(wal) == 0) {
        printf("  WAL manager started successfully\n");
    } else {
        printf("  Failed to start WAL manager\n");
        all_passed = false;
        wal_manager_destroy(wal);
        cleanup_test_directory(test_wal_dir);
        return false;
    }
    
    // Test transaction begin logging
    printf("Testing transaction begin logging:\n");
    lsn_t begin_lsn = wal_log_transaction_begin(wal, 1001, ISOLATION_READ_COMMITTED);
    if (begin_lsn != INVALID_LSN) {
        printf("  Logged transaction begin: LSN %" PRIu64 "\n", begin_lsn);
    } else {
        printf("  Failed to log transaction begin\n");
        all_passed = false;
    }
    
    // Test insert logging
    printf("Testing insert logging:\n");
    const char* tuple_data = "user_id=123,name=Alice,age=25";
    lsn_t insert_lsn = wal_log_insert(wal, 1001, 1, 10, 1001, 
                                     tuple_data, strlen(tuple_data));
    if (insert_lsn != INVALID_LSN) {
        printf("  Logged insert: LSN %" PRIu64 "\n", insert_lsn);
    } else {
        printf("  Failed to log insert\n");
        all_passed = false;
    }
    
    // Test update logging
    printf("Testing update logging:\n");
    const char* old_data = "user_id=123,name=Alice,age=25";
    const char* new_data = "user_id=123,name=Alice,age=26";
    lsn_t update_lsn = wal_log_update(wal, 1001, 1, 10, 1001, 
                                     old_data, strlen(old_data),
                                     new_data, strlen(new_data));
    if (update_lsn != INVALID_LSN) {
        printf("  Logged update: LSN %" PRIu64 "\n", update_lsn);
    } else {
        printf("  Failed to log update\n");
        all_passed = false;
    }
    
    // Test delete logging
    printf("Testing delete logging:\n");
    lsn_t delete_lsn = wal_log_delete(wal, 1001, 1, 10, 1001,
                                     new_data, strlen(new_data));
    if (delete_lsn != INVALID_LSN) {
        printf("  Logged delete: LSN %" PRIu64 "\n", delete_lsn);
    } else {
        printf("  Failed to log delete\n");
        all_passed = false;
    }
    
    // Test transaction commit logging
    printf("Testing transaction commit logging:\n");
    lsn_t commit_lsn = wal_log_transaction_commit(wal, 1001);
    if (commit_lsn != INVALID_LSN) {
        printf("  Logged transaction commit: LSN %" PRIu64 "\n", commit_lsn);
    } else {
        printf("  Failed to log transaction commit\n");
        all_passed = false;
    }
    
    // Test LSN functions
    printf("Testing LSN functions:\n");
    lsn_t current_lsn = wal_get_current_lsn(wal);
    lsn_t flushed_lsn = wal_get_flushed_lsn(wal);
    printf("  Current LSN: %" PRIu64 "\n", current_lsn);
    printf("  Flushed LSN: %" PRIu64 "\n", flushed_lsn);
    
    if (current_lsn < commit_lsn) {
        printf("  Current LSN should be >= commit LSN\n");
        all_passed = false;
    }
    
    // Sleep briefly to allow background threads to work
    printf("Waiting for background processing...\n");
    sleep(2);
    
    // Test flush operation
    printf("Testing flush operation:\n");
    if (wal_flush_to_lsn(wal, commit_lsn) == 0) {
        printf("  Flushed to LSN %" PRIu64 ": SUCCESS\n", commit_lsn);
    } else {
        printf("  Failed to flush to LSN %" PRIu64 "\n", commit_lsn);
        all_passed = false;
    }
    
    // Test sync operation
    printf("Testing sync operation:\n");
    if (wal_sync_to_lsn(wal, commit_lsn) == 0) {
        printf("  Synced to LSN %" PRIu64 ": SUCCESS\n", commit_lsn);
    } else {
        printf("  Failed to sync to LSN %" PRIu64 "\n", commit_lsn);
        all_passed = false;
    }
    
    // Print statistics
    printf("WAL statistics after operations:\n");
    wal_print_stats(wal);
    
    // Stop WAL manager
    printf("Stopping WAL manager:\n");
    if (wal_manager_stop(wal) == 0) {
        printf("  WAL manager stopped successfully\n");
    } else {
        printf("  Failed to stop WAL manager\n");
        all_passed = false;
    }
    
    wal_manager_destroy(wal);
    cleanup_test_directory(test_wal_dir);
    
    return all_passed;
}

// Test checkpoint operations
static bool test_checkpoint_operations(void) {
    print_test_header("Testing Checkpoint Operations");
    
    const char* test_wal_dir = "/tmp/test_wal_checkpoint";
    create_test_directory(test_wal_dir);
    
    wal_manager_t* wal = wal_manager_create(test_wal_dir);
    if (!wal) {
        printf("Failed to create WAL manager\n");
        cleanup_test_directory(test_wal_dir);
        return false;
    }
    
    bool all_passed = true;
    
    if (wal_manager_start(wal) != 0) {
        printf("Failed to start WAL manager\n");
        wal_manager_destroy(wal);
        cleanup_test_directory(test_wal_dir);
        return false;
    }
    
    // Log some operations first
    printf("Logging operations before checkpoint:\n");
    wal_log_transaction_begin(wal, 2001, ISOLATION_REPEATABLE_READ);
    wal_log_insert(wal, 2001, 2, 20, 2001, "test_data_1", 11);
    wal_log_insert(wal, 2001, 2, 20, 2002, "test_data_2", 11);
    wal_log_transaction_commit(wal, 2001);
    
    // Test checkpoint creation
    printf("Testing checkpoint creation:\n");
    txn_id_t active_xids[] = {3001, 3002, 3003};
    uint32_t active_count = 3;
    
    lsn_t checkpoint_lsn = wal_create_checkpoint(wal, active_xids, active_count);
    if (checkpoint_lsn != INVALID_LSN) {
        printf("  Created checkpoint: LSN %" PRIu64 "\n", checkpoint_lsn);
        printf("  Checkpoint LSN stored: %" PRIu64 "\n", wal->checkpoint_lsn);
        
        if (wal->checkpoint_lsn != checkpoint_lsn) {
            printf("  Checkpoint LSN not stored correctly\n");
            all_passed = false;
        }
    } else {
        printf("  Failed to create checkpoint\n");
        all_passed = false;
    }
    
    // Test checkpoint with no active transactions
    printf("Testing checkpoint with no active transactions:\n");
    lsn_t checkpoint_lsn2 = wal_create_checkpoint(wal, NULL, 0);
    if (checkpoint_lsn2 != INVALID_LSN) {
        printf("  Created empty checkpoint: LSN %" PRIu64 "\n", checkpoint_lsn2);
    } else {
        printf("  Failed to create empty checkpoint\n");
        all_passed = false;
    }
    
    // Test checkpoint restore (simplified)
    printf("Testing checkpoint restore:\n");
    checkpoint_info_t* restored_checkpoint = NULL;
    if (wal_restore_checkpoint(wal, &restored_checkpoint) == 0) {
        printf("  Checkpoint restore function called successfully\n");
        printf("  (Note: Actual restore not implemented in this test)\n");
    } else {
        printf("  Failed to call checkpoint restore function\n");
        all_passed = false;
    }
    
    wal_manager_stop(wal);
    wal_manager_destroy(wal);
    cleanup_test_directory(test_wal_dir);
    
    return all_passed;
}

// Test segment management
static bool test_segment_management(void) {
    print_test_header("Testing Segment Management");
    
    const char* test_wal_dir = "/tmp/test_wal_segments";
    create_test_directory(test_wal_dir);
    
    wal_manager_t* wal = wal_manager_create(test_wal_dir);
    if (!wal) {
        printf("Failed to create WAL manager\n");
        cleanup_test_directory(test_wal_dir);
        return false;
    }
    
    bool all_passed = true;
    
    if (wal_manager_start(wal) != 0) {
        printf("Failed to start WAL manager\n");
        wal_manager_destroy(wal);
        cleanup_test_directory(test_wal_dir);
        return false;
    }
    
    // Test initial segment
    printf("Testing initial segment:\n");
    if (wal->active_segment) {
        printf("  Active segment created: ID %u\n", wal->active_segment->segment_id);
        printf("  Segment filename: %s\n", wal->active_segment->filename);
        printf("  Segment is active: %s\n", wal->active_segment->is_active ? "true" : "false");
        printf("  Segment start LSN: %" PRIu64 "\n", wal->active_segment->start_lsn);
    } else {
        printf("  No active segment found\n");
        all_passed = false;
    }
    
    // Test segment statistics
    printf("Testing segment statistics:\n");
    int segment_count = wal_get_segment_count(wal);
    uint64_t total_size = wal_get_total_size(wal);
    printf("  Segment count: %d\n", segment_count);
    printf("  Total size: %" PRIu64 " bytes\n", total_size);
    
    if (segment_count < 1) {
        printf("  Should have at least one segment\n");
        all_passed = false;
    }
    
    // Log many operations to potentially trigger segment creation
    printf("Logging many operations to test segment growth:\n");
    for (int i = 0; i < 100; i++) {
        char data[256];
        snprintf(data, sizeof(data), "Large test data record number %d with lots of content to fill up the WAL buffer and potentially create new segments", i);
        
        wal_log_transaction_begin(wal, 4000 + i, ISOLATION_READ_COMMITTED);
        wal_log_insert(wal, 4000 + i, 4, 40 + i, 4000 + i, data, strlen(data));
        wal_log_transaction_commit(wal, 4000 + i);
        
        if (i % 20 == 19) {
            printf("  Logged %d operations\n", i + 1);
        }
    }
    
    // Check statistics after logging
    printf("Segment statistics after heavy logging:\n");
    segment_count = wal_get_segment_count(wal);
    total_size = wal_get_total_size(wal);
    printf("  Final segment count: %d\n", segment_count);
    printf("  Final total size: %" PRIu64 " bytes\n", total_size);
    
    // Force sync to ensure everything is written
    printf("Force syncing WAL:\n");
    if (wal_force_sync(wal) == 0) {
        printf("  Force sync completed successfully\n");
    } else {
        printf("  Force sync failed\n");
        all_passed = false;
    }
    
    wal_manager_stop(wal);
    wal_manager_destroy(wal);
    cleanup_test_directory(test_wal_dir);
    
    return all_passed;
}

// Test error conditions
static bool test_error_conditions(void) {
    print_test_header("Testing Error Conditions");
    
    bool all_passed = true;
    
    // Test NULL parameter handling
    printf("Testing NULL parameter handling:\n");
    
    if (wal_manager_create(NULL) == NULL) {
        printf("  wal_manager_create correctly rejects NULL directory\n");
    } else {
        printf("  wal_manager_create should reject NULL directory\n");
        all_passed = false;
    }
    
    if (wal_insert_record(NULL, WAL_INSERT, 1, "data", 4) == INVALID_LSN) {
        printf("  wal_insert_record correctly rejects NULL manager\n");
    } else {
        printf("  wal_insert_record should reject NULL manager\n");
        all_passed = false;
    }
    
    if (wal_record_create(WAL_INSERT, 1, NULL, 10) == NULL) {
        printf("  wal_record_create correctly rejects NULL data with non-zero size\n");
    } else {
        printf("  wal_record_create should reject NULL data with non-zero size\n");
        all_passed = false;
    }
    
    // Test invalid directory
    printf("Testing invalid directory:\n");
    wal_manager_t* wal = wal_manager_create("/invalid/nonexistent/directory/path");
    if (wal == NULL) {
        printf("  WAL manager correctly rejects invalid directory\n");
    } else {
        printf("  WAL manager should reject invalid directory\n");
        wal_manager_destroy(wal);
        all_passed = false;
    }
    
    // Test record size limits
    printf("Testing record size limits:\n");
    const char* test_wal_dir = "/tmp/test_wal_errors";
    create_test_directory(test_wal_dir);
    
    wal = wal_manager_create(test_wal_dir);
    if (wal) {
        if (wal_manager_start(wal) == 0) {
            // Try to log a record that's too large
            char* large_data = malloc(WAL_MAX_RECORD_SIZE + 1000);
            if (large_data) {
                memset(large_data, 'X', WAL_MAX_RECORD_SIZE + 999);
                large_data[WAL_MAX_RECORD_SIZE + 999] = '\0';
                
                lsn_t large_lsn = wal_insert_record(wal, WAL_INSERT, 1, 
                                                   large_data, WAL_MAX_RECORD_SIZE + 999);
                if (large_lsn == INVALID_LSN) {
                    printf("  Correctly rejected oversized record\n");
                } else {
                    printf("  Should have rejected oversized record\n");
                    all_passed = false;
                }
                
                free(large_data);
            }
            
            wal_manager_stop(wal);
        }
        wal_manager_destroy(wal);
    }
    
    cleanup_test_directory(test_wal_dir);
    
    return all_passed;
}

// Test utility functions
static bool test_utility_functions(void) {
    print_test_header("Testing Utility Functions");
    
    bool all_passed = true;
    
    // Test record type to string conversion
    printf("Testing record type to string conversion:\n");
    printf("  WAL_INVALID: %s\n", wal_record_type_to_string(WAL_INVALID));
    printf("  WAL_INSERT: %s\n", wal_record_type_to_string(WAL_INSERT));
    printf("  WAL_UPDATE: %s\n", wal_record_type_to_string(WAL_UPDATE));
    printf("  WAL_DELETE: %s\n", wal_record_type_to_string(WAL_DELETE));
    printf("  WAL_TRANSACTION_BEGIN: %s\n", wal_record_type_to_string(WAL_TRANSACTION_BEGIN));
    printf("  WAL_TRANSACTION_COMMIT: %s\n", wal_record_type_to_string(WAL_TRANSACTION_COMMIT));
    printf("  WAL_CHECKPOINT: %s\n", wal_record_type_to_string(WAL_CHECKPOINT));
    
    // Test CRC32 calculation
    printf("Testing CRC32 calculation:\n");
    const char* test_data = "Hello, CRC32!";
    uint32_t crc1 = wal_calculate_crc32(test_data, strlen(test_data));
    uint32_t crc2 = wal_calculate_crc32(test_data, strlen(test_data));
    
    printf("  CRC32 of '%s': 0x%08X\n", test_data, crc1);
    
    if (crc1 == crc2) {
        printf("  CRC32 calculation is consistent\n");
    } else {
        printf("  CRC32 calculation is inconsistent\n");
        all_passed = false;
    }
    
    // Test with different data
    const char* test_data2 = "Hello, CRC32?";  // Note the different ending
    uint32_t crc3 = wal_calculate_crc32(test_data2, strlen(test_data2));
    
    if (crc1 != crc3) {
        printf("  CRC32 correctly produces different values for different data\n");
    } else {
        printf("  CRC32 should produce different values for different data\n");
        all_passed = false;
    }
    
    return all_passed;
}

int main() {
    printf("WAL Manager Test Suite\n");
    printf("=====================\n");
    
    int passed = 0;
    int total = 0;
    
    // Run all tests
    struct {
        const char* name;
        bool (*test_func)(void);
    } tests[] = {
        {"WAL Manager Creation", test_wal_manager_creation},
        {"WAL Manager Configuration", test_wal_manager_configuration},
        {"WAL Record Management", test_wal_record_management},
        {"WAL Buffer Operations", test_wal_buffer_operations},
        {"Basic WAL Operations", test_basic_wal_operations},
        {"Checkpoint Operations", test_checkpoint_operations},
        {"Segment Management", test_segment_management},
        {"Error Conditions", test_error_conditions},
        {"Utility Functions", test_utility_functions},
        {NULL, NULL}
    };
    
    for (int i = 0; tests[i].name != NULL; i++) {
        total++;
        bool result = tests[i].test_func();
        if (result) {
            passed++;
        }
        print_test_result(tests[i].name, result);
    }
    
    printf("\n=== Test Summary ===\n");
    printf("Passed: %d/%d\n", passed, total);
    printf("Success rate: %.1f%%\n", (double)passed / total * 100.0);
    
    return (passed == total) ? 0 : 1;
}